<?
class sym_voting_admin extends symbiont{
    public function ___main($design=''){
        global $sql, $kernel;
        if($voting=$kernel->link->param2){
            $design=$this->_check($design, 'admin_variants', 'voting');
            $d=new design($design, 'voting_admin', false);
            $voting_id=$sql->select_each('sym_voting', 'id', array('name'=>$voting), '', '', 1);
            $items=$sql->query_each("
                            SELECT v.id, l.value as variant
                            FROM `sym_voting_variants` as v
                                LEFT JOIN `labels` as l ON v.variant_lid = l.id
                            WHERE v.voting_id = ".$voting_id."
                            ORDER BY position
                            ");
            $this->items='';
            $this->voting_id=$voting_id;
            if(is_array($items))
            foreach($items as $item){
                $this->id=$item['id'];
                $this->variant=$item['variant'];
                $this->items.=design::run($this->item);
            }
            $d->inc();
        }
        else{
            $design=$this->_check($design, 'admin', 'voting');
            $d=new design($design, 'voting_admin', false);
            $items=$sql->query_each("
                            SELECT v.id, v.name, l.value as question
                            FROM `sym_voting` as v
                                LEFT JOIN `labels` as l ON v.question_lid = l.id
                            ORDER BY question");
            $this->items='';
            if(is_array($items))
            foreach($items as $item){
                $this->id=$item['id'];
                $this->name=$item['name'];
                $this->question=$item['question']?$item['question']:$item['name'];
                $this->items.=design::run($this->item);
            }
            $d->inc();
        }
    }
    public function ___add($design=''){
        global $sql;
        $design=$this->_check($design, 'add', 'voting');
        $d=new design($design, 'voting_admin', false);
        
        $this->name='';
        $this->val='';
        $this->infos='';
        $this->languages='';
        $languages=$sql->select_each('languages', array('abbr', 'id'), array('is_enabled'=>1), '', 'position');
        foreach($languages as $val){
            $this->id=$val['id'];
            $this->abbr=$val['abbr'];
            $this->languages.=design::run($this->language);
            $this->infos.=design::run($this->info);
        }
        
        $d->inc();
    }
    public function ___edit($design='', $id=''){
        if(!data::is_uint($id)){
            return;
        }
        global $sql;
        $design=$this->_check($design, 'add', 'voting');
        $d=new design($design, 'voting_admin', false);
        
        $item=$sql->query_each("
                        SELECT name, question_lid
                        FROM `sym_voting` as v
                        WHERE `id` = ".$id, true);
        $this->name=$item['name'];
        $this->infos='';
        $this->languages='';
        $languages=$sql->select_each('languages', array('abbr', 'id'), array('is_enabled'=>1), '', 'position');
        
        $l=new label($item['question_lid']);
        foreach($languages as $val){
            $this->id=$val['id'];
            $this->abbr=$val['abbr'];
            $this->val=$l->get($val['id']);
            $this->languages.=design::run($this->language);
            $this->infos.=design::run($this->info);
        }
        
        $d->inc();
    }
    public function ___delete($design=''){
        $design=$this->_check($design, 'delete', 'voting');
        $d=new design($design, 'voting_admin');
    }
    
    public function ___variant_add($design=''){
        global $sql;
        $design=$this->_check($design, 'variant_add', 'voting');
        $d=new design($design, 'voting_admin', false);
        
        $this->name='';
        $this->val='';
        $this->infos='';
        $this->languages='';
        $languages=$sql->select_each('languages', array('abbr', 'id'), array('is_enabled'=>1), '', 'position');
        foreach($languages as $val){
            $this->id=$val['id'];
            $this->abbr=$val['abbr'];
            $this->languages.=design::run($this->language);
            $this->infos.=design::run($this->info);
        }
        
        $d->inc();
    }
    
    public function ___variant_edit($design='', $voting_id='', $id=''){
        global $sql;
        if(!data::is_uint($id)||!data::is_uint($voting_id)){
            return;
        }
        $design=$this->_check($design, 'variant_add', 'voting');
        $d=new design($design, 'voting_admin', false);
        
        $variant_lid=$sql->select_each('sym_voting_variants', 'variant_lid', array('id'=>$id, 'voting_id'=>$voting_id), '', '', 1);
        
        $this->name='';
        $this->infos='';
        $this->languages='';
        $l=new label($variant_lid);
        $languages=$sql->select_each('languages', array('abbr', 'id'), array('is_enabled'=>1), '', 'position');
        foreach($languages as $val){
            $this->id=$val['id'];
            $this->abbr=$val['abbr'];
            $this->val=$l->get($val['id']);
            $this->languages.=design::run($this->language);
            $this->infos.=design::run($this->info);
        }
        
        $d->inc();
    }
    
    public function ___variant_delete($design='', $voting_id='', $id=''){
        $design=$this->_check($design, 'variant_delete', 'voting');
        $d=new design($design, 'voting_admin');
    }
    
    public function ___db_add($design=''){
        global $sql, $user;
        if($user->access_level<9){
            print('{"error":"You have not enought prerogatives."}');
            return;
        }
        if(!isset($_POST['name'])){
            print('{"error":"Not the all parametrs have been sent."}');
            return;
        }
        $name=data::word($_POST['name']);
        if($name===''){
            print('{"error":"Name is empty."}');
            return;
        }
        $id=$sql->insert('sym_voting', array('name'=>$name));
        if(!$id){
            print('{"error":"Adding error. Maybe voting with this name already exists."}');
            return;
        }
        $question_lid=0;
        if(isset($_POST['question'])){
            $l=new label();
            foreach($_POST['question'] as $key=>$val){
                $val=data::safe($val);
                $key=data::word($key, 'en', 2);
                $l->add($val, $key);
            }
            $question_lid=$l->id;
        }
        $sql->update('sym_voting', array('question_lid'=>$question_lid), array('id'=>$id));
        print('{"ok":"Voting added."}');
    }
    public function ___db_edit($design=''){
        global $sql, $user;
        if($user->access_level<9){
            print('{"error":"You have not enought prerogatives."}');
            return;
        }
        if(!isset($_POST['name'])&&!isset($_POST['id'])){
            print('{"error":"Not the all parametrs have been sent."}');
            return;
        }
        $name=data::word($_POST['name']);
        if($name===''){
            print('{"error":"Name is empty."}');
            return;
        }
        $id=$_POST['id'];
        if(!data::is_uint($id)){
            print('{"error":"Id most be unsigned int."}');
            return;
        }
        $r=$sql->update('sym_voting', array('name'=>$name), array('id'=>$id));
        if(!$r){
            print('{"error":"Editing error. Maybe voting with this name already exists."}');
            return;
        }
        
        $question_lid=$sql->select_each('sym_voting', 'question_lid', array('id'=>$id), '', '', 1);
        if(isset($_POST['question'])){
            $l=new label($question_lid);
            foreach($_POST['question'] as $key=>$val){
                $val=data::safe($val);
                $key=data::word($key, 'en', 2);
                $l->set($val, $key);
            }
            $question_lid=$l->id;
        }
        
        $sql->update('sym_voting', array('question_lid'=>$question_lid), array('id'=>$id));
        print('{"ok":"Voting edited."}');
        
    }
    public function ___db_delete($design=''){
        global $sql, $user;
        if($user->access_level<9){
            print('{"error":"You have not enought prerogatives."}');
            return;
        }
        if(!isset($_POST['id'])){
            print('{"error":"Not the all parametrs have been sent."}');
            return;
        }
        $id=$_POST['id'];
        if(!data::is_uint($id)){
            print('{"error":"Id most be unsigned int."}');
            return;
        }
        
        $variants=$sql->select_each('sym_voting_variants', 'variant_lid', array('voting_id'=>$id));
        if(is_array($variants))
        foreach($variants as $variant){
            $l=new label($variant['variant_lid']);
            $l->del();
        }
        $sql->delete('sym_voting_variants', array('voting_id'=>$id));
        
        $question_lid=$sql->select_each('sym_voting', 'question_lid', array('id'=>$id), '', '', 1);
        $l=new label($question_lid);
        $l->del();
        $sql->delete('sym_voting', array('id'=>$id), 1);
        print('{"ok":"Menu deleted."}');
    }
    public function ___db_variant_add($design=''){
        global $sql, $user;
        if($user->access_level<9){
            print('{"error":"You have not enought prerogatives."}');
            return;
        }
        if(!isset($_POST['voting_id'])){
            print('{"error":"Not the all parametrs have been sent."}');
            return;
        }
        $voting_id=$_POST['voting_id'];
        if(!data::is_uint($voting_id)){
            print('{"error":"Incorrect parametr type."}');
            return;
        }
        $variant_lid=0;
        if(isset($_POST['variant'])){
            $l=new label();
            foreach($_POST['variant'] as $key=>$val){
                $val=data::safe($val);
                $key=data::word($key, 'en', 2);
                $l->add($val, $key);
            }
            $variant_lid=$l->id;
        }
        $id=$sql->insert('sym_voting_variants', array('voting_id'=>$voting_id, 'variant_lid'=>$variant_lid));
        if($id){
            print('{"ok":"Voting added."}');            
            $sql->update('sym_voting_variants', array('position'=>$id), array('id'=>$id), 1);
        }
        else print('{"error":"Adding error."}');
    }
    
    public function ___db_variant_edit($design=''){
        global $sql, $user;
        if($user->access_level<9){
            print('{"error":"You have not enought prerogatives."}');
            return;
        }
        if(!isset($_POST['voting_id'])&&!isset($_POST['id'])){
            print('{"error":"Not the all parametrs have been sent."}');
            return;
        }
        $id=$_POST['id'];
        $voting_id=$_POST['voting_id'];
        if(!data::is_uint($id)||!data::is_uint($voting_id)){
            print('{"error":"Incorrect parametr type."}');
            return;
        }
        
        $variant_lid=$sql->select_each('sym_voting_variants', 'variant_lid', array('id'=>$id, 'voting_id'=>$voting_id));;
        if(isset($_POST['variant'])){
            $l=new label($variant_lid);
            foreach($_POST['variant'] as $key=>$val){
                $val=data::safe($val);
                $key=data::word($key, 'en', 2);
                $l->set($val, $key);
            }
            $variant_lid=$l->id;
        }
        
        $sql->update('sym_voting_variants', array('variant_lid'=>$variant_lid), array('id'=>$id, 'voting_id'=>$voting_id));
        print('{"ok":"Variant edited."}');
    }
    
    public function ___db_variant_delete($design=''){
        global $sql, $user;
        if($user->access_level<9){
            print('{"error":"You have not enought prerogatives."}');
            return;
        }
        if(!isset($_POST['id'])||!isset($_POST['voting_id'])){
            print('{"error":"Not the all parametrs have been sent."}');
            return;
        }
        $id=$_POST['id'];
        $voting_id=$_POST['voting_id'];
        if(!data::is_uint($id)&&!data::is_uint($voting_id)){
            print('{"error":"Incorrect parametr type."}');
            return;
        }
        $variant_lid=$sql->select_each('sym_voting_variants', 'variant_lid', array('voting_id'=>$voting_id, 'id'=>$id), '', '', 1);
        $l=new label($variant_lid);
        $l->del();
        $r=$sql->delete('sym_voting_variants', array('voting_id'=>$voting_id, 'id'=>$id), 1);
        if($r) print('{"ok":"Variant deleted."}');
        else print('{"error":"Variant deleting error."}');
    }
    
    public function ___db_sort($design=''){
        global $user, $sql;
        if($user->access_level<9){
            print('{"error":"You have not enought prerogatives."}');
            return;
        }
        if(!isset($_POST['sort'])||!isset($_POST['voting_id'])){
            print('{"error":"Not the all parametrs have been sent."}');
            return;
        }
        $voting_id=$_POST['voting_id'];
        if(!data::is_uint($voting_id)){
            print('{"error":"Incorrect parametr type."}');
            return;
        }
        foreach($_POST['sort'] as $key=>$val){
            $id=data::number($val);
            $position=data::number($key)+1;
            $sql->update('sym_voting_variants', array('position'=>$position), array('id'=>$id, 'voting_id'=>$voting_id), 1);
        }
    }
}
?>